Questions for Max:

  1. Do I need to install a package at the beginning of every RMarkdown file? eg. lubridate
  2. Do I need to call the library at the beginning of every chunk of code?

Before Starting: load libraries

library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.7     ✔ stringr 1.4.0
## ✔ tidyr   1.2.0     ✔ forcats 0.5.1
## ✔ readr   2.1.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ lubridate::as.difftime() masks base::as.difftime()
## ✖ lubridate::date()        masks base::date()
## ✖ dplyr::filter()          masks stats::filter()
## ✖ lubridate::intersect()   masks base::intersect()
## ✖ dplyr::lag()             masks stats::lag()
## ✖ lubridate::setdiff()     masks base::setdiff()
## ✖ lubridate::union()       masks base::union()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout

Task #1: Read the file into a dataframe.

Step 1: load the data.

depth_data <- read.delim("Depth_Asgeir.txt")
#depth_data <- read_delim("Depth_Asgeir.txt")
header <- head(depth_data)
class(depth_data) #checks that the file has been read as a dataframe.
## [1] "data.frame"

Task #2: Convert the datetime column from character to POSIXct.

Step 1: Outline changes that need to happen.

Format of datetime entry in data:
m/dd/yy hh:mm:ss parse_datetime() expects: yyyy/mm/dd hh:mm:ss

Step 2: Figure out how we need to manipulate the given date using the date from the first entry

line_one <- "8/23/18 22:46:01"
dt_conversion <- "m/%d/%y %H:%M:%S"
parse_date_time(line_one, dt_conversion)
## [1] "2018-08-23 22:46:01 UTC"

Step 3: Test it on the header. I’m pretty sure there’s a better way to do this, but I haven’t figured it out.

header_datetime <- pull(header, "Date.time") #create a vector with first 6 datetimes
UTC_date_time <- parse_date_time(header_datetime, dt_conversion) #vector with first 6 datetimes in UTC
UTC_df <- data.frame(UTC_date_time) #turn the vector back into a column 
UTC_header <- mutate(header, UTC_df, .before = "Date.time") #adds the UTC date time column into the header df before the old datetime column 
select(UTC_header, -c("Date.time")) #Delete the old datetime column 
##         UTC_date_time Depth Clicking Buzzing
## 1 2018-08-23 22:46:00   0.8      NaN     NaN
## 2 2018-08-23 22:46:01   1.0      NaN     NaN
## 3 2018-08-23 22:46:02   1.1      NaN     NaN
## 4 2018-08-23 22:46:03   1.2      NaN     NaN
## 5 2018-08-23 22:46:04   1.1      NaN     NaN
## 6 2018-08-23 22:46:05   1.1      NaN     NaN

Step 4: Apply this set of functions to the whole dataset.

datetime_col <- pull(depth_data, "Date.time") #creates a vector with all datetimes
UTC_vec <- parse_date_time(datetime_col, dt_conversion) #changes into UTC
UTC_col <- data.frame(UTC_vec) #changes it back into a df 
depth_data_UTC <- mutate(depth_data, UTC_col, .before = "Date.time") #Adds UTC datetime column in before old date time column 
depth_data_UTC <- select(depth_data_UTC, -c("Date.time")) #delete old datetime column
depth_data_UTC <- rename(depth_data_UTC, DateTime = UTC_vec) #renames the datetime column 

Step 5: Print header to show the new table

head(depth_data_UTC)
##              DateTime Depth Clicking Buzzing
## 1 2018-08-23 22:46:00   0.8      NaN     NaN
## 2 2018-08-23 22:46:01   1.0      NaN     NaN
## 3 2018-08-23 22:46:02   1.1      NaN     NaN
## 4 2018-08-23 22:46:03   1.2      NaN     NaN
## 5 2018-08-23 22:46:04   1.1      NaN     NaN
## 6 2018-08-23 22:46:05   1.1      NaN     NaN

Task #3 Make Figures

  1. Depth profile for the whole deployment
  2. Depth profile for 1 day
  3. Depth profile for 1 dive

Step 1: Make a plot for the whole deployment

deployment_plot <- ggplot(depth_data_UTC, aes(x = DateTime, y = Depth)) + 
  geom_line() +
  scale_y_reverse() + #makes depth right orientation 
  labs(title = "Asgeir Depth Profile 2018",
       x = "Time",
       y = "Depth (m)") + #changes labels on axes and title 
  theme_bw()  #white background 

Step 2: Make a plot for 1 day

I’m a little confused on this one - This is a 24 hour period, but it doesn’t start at midnight, and I think we might need to know the timezone for this one.

start_day <- as.POSIXct('2018-08-24 00:00:00')
end_day <- as.POSIXct('2018-08-24 23:59:59')
one_day <- dplyr::filter(depth_data_UTC, DateTime >= start_day, DateTime <= end_day)
day_plot<- ggplot(one_day, aes(x = DateTime, y = Depth)) + #filters datetime to 24 hour period
  geom_line() +
  scale_y_reverse() + #makes depth right orientation 
  labs(title = "Asgeir 24 Hour Depth Profile 2018",
       x = "Time",
       y = "Depth (m)") + #changes labels on axes and title 
  theme_bw()  #white background 
ggplotly(day_plot) #makes plot interactive 

Step 3: Make a plot for 1 dive

Also have to figure out some time zone stuff on this one.

start_dive_time <- as.POSIXct('2018-08-24 15:26:01') #selected times from interactive day plot 
end_dive_time <- as.POSIXct('2018-08-24 15:49:04')
one_dive <- dplyr::filter(depth_data_UTC, DateTime >= start_dive_time, DateTime <= end_dive_time)
dive_plot <- ggplot(one_dive, aes(x = DateTime, y = Depth)) + #filters datetime to 24 hour period
  geom_line() +
  scale_y_reverse() + #makes depth right orientation 
  labs(title = "Asgeir Single Dive Depth Profile 2018",
       x = "Time",
       y = "Depth (m)") + #changes labels on axes and title 
  theme_bw()  #white background 
ggplotly(dive_plot)